﻿using Dapper;
using System;
using System.Linq;
using SilenthinkContract;
using SilenthinkContract.Domain;
using System.Collections.Generic;

namespace SilenthinkService
{
    public class LogService : ServiceBase, ILogService
    {
        public AppError GetAppError(int id)
        {
            var builder = new SqlBuilder().Field("*")
                .Table(Database.Log.Table.AppException)
                .Where("[Id]=@Id", new {id});

            var template = builder.AddTemplate(SqlTemplate.SelectTopOne);
            using (var connection = GetConnection(ConnectionName.Log))
            {
                return Query<AppError>(connection, template).FirstOrDefault();
            }
        }

        public bool TruncateAppError()
        {
            var builder = new SqlBuilder()
                .Table(Database.Log.Table.AppException);

            var template = builder.AddTemplate("truncate table /**table**/");
            using (var connection = GetConnection(ConnectionName.Log))
            {
                Execute(connection, template);
                return true;
            }
        }

        public PageList<AppError> GetPagedAppError(int index = 1, int size = 20, byte level = 0, string machineIp = null,
                                                   string userName = null, string clientIp = null)
        {
            var builder = new SqlBuilder().Field(
                "[Id],[Machine],[Server],[ServerIp],[Level],[ExType],[ExSource],[ExMessage],[ClientIp],[CreatedBy],[CreatedOn]")
                .Table(Database.Log.Table.AppException)
                .OrderBy("id desc")
                .AddParameters(new {begin = (index - 1)*size + 1, end = index*size});
            if (level > 0)
            {
                builder.Where("[Level]=@Level", new {level});
            }
            if (!string.IsNullOrEmpty(machineIp))
            {
                builder.Where("([Machine]=@MachineIp OR [ServerIp]=@MachineIp)", new {machineIp});
            }
            if (!string.IsNullOrEmpty(userName))
            {
                builder.Where("[CreatedBy]=@CreatedBy", new {CreatedBy = userName});
            }
            if (!string.IsNullOrEmpty(clientIp))
            {
                builder.Where("[ClientIp]=@ClientIp", new {clientIp});
            }

            var template = builder.AddTemplate(SqlTemplate.PagedSelect);
            using (var connection = GetConnection(ConnectionName.Log))
            {
                return PageList<AppError>(connection, template);
            }
        }

        #region Implementation of IService

        public int GetCachedSQLCount()
        {
            return SqlMapper.GetCachedSQLCount();
        }

        public List<Tuple<string, string, int>> GetCachedSQL()
        {
            return SqlMapper.GetCachedSQL().ToList();
        }

        public void PurgeQueryCache()
        {
            SqlMapper.PurgeQueryCache();
        }

        #endregion

    }
}